using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Fanfou
{
    /// <summary>
    /// When a FanfouRequest started. it need a FanfouSession.
    /// </summary>
    [Serializable]
    public class FanfouSession : ISerializable
    {
        private readonly string _id, _loginname, _password;

        [NonSerialized]
        private NetworkCredential _credential;

        /// <summary>
        /// User's Id
        /// </summary>
        public string Id
        {
            get { return _id; }
        }

        /// <summary>
        /// The Request credential
        /// </summary>
        public NetworkCredential Credential
        {
            get
            {
                return _credential;
            }
        }

        /// <summary>
        /// Create a request session to use
        /// </summary>
        /// <param name="id">user's id</param>
        /// <param name="logginname">user's loginname</param>
        /// <param name="password">user's password</param>
        public FanfouSession(string id, string logginname, string password)
        {
            _id = id;
            _loginname = logginname;
            _password = password;

            _credential = new NetworkCredential(logginname, password);
        }

        private FanfouSession(SerializationInfo info, StreamingContext context)
        {
            _id = info.GetString("_id");
            _loginname = info.GetString("_loginname");
            _password = info.GetString("_password");

            _credential = new NetworkCredential(_loginname, _password);
        }

        [SecurityPermissionAttribute(SecurityAction.LinkDemand,Flags=SecurityPermissionFlag.SerializationFormatter)]
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("_id", _id);
            info.AddValue("_loginname", _loginname);
            info.AddValue("_password", _password);
        }
    }
}
